/** 
 * This file is part of WGA.
 * 
 * WGA is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * WGA is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with WGA.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Copyright 2010, 2011 Omar Andres Carmona Cortes, Roberto Leite de Moraes Rego Fihlo
 * 						omar@ifma.edu.br			robertoleite1@gmail.com
 */

package util;

import componentes.Individuo;

public class Hamming {
	private double[] compOne;
	private double[] compTwo;

	public Hamming(double[] one, double[] two) {
		compOne = one;
		compTwo = two;
	}

	// /
	// Calculating the Hamming Distance for two strings requires the string to
	// be of the same length.
	// /
	public int getHammingDistance() {
		if (compOne.length != compTwo.length) {
			return -1;
		}

		int counter = 0;

		for (int i = 0; i < compOne.length; i++) {
			if (compOne[i] != compTwo[i])
				counter++;
		}

		return counter;
	}

	// /
	// Hamming distance works best with binary comparisons, this function takes
	// a string arrary of binary
	// values and returns the minimum distance value
	// /
	public int minDistance(String[] numbers) {
		int minDistance = Integer.MAX_VALUE;

		if (checkConstraints(numbers)) {
			for (int i = 1; i < numbers.length; i++) {
				int counter = 0;
				for (int j = 1; j <= numbers[i].length(); j++) {
					if (numbers[i - 1].charAt(j - 1) != numbers[i]
							.charAt(j - 1)) {
						counter++;
					}
				}

				if (counter == 0)
					return counter;
				if (counter < minDistance)
					minDistance = counter;
			}
		} else {
			return -1;
		}

		return minDistance;
	}

	private Boolean checkConstraints(String[] numbers) {
		if (numbers.length > 1 && numbers.length <= 50) {
			int prevLength = -1;
			for (int i = 0; i < numbers.length; i++) {
				if (numbers[i].length() > 0 && numbers[i].length() <= 50) {
					if (prevLength == -1) {
						prevLength = numbers[i].length();
					} else {
						if (prevLength != numbers[i].length()) {
							return false;
						}
					}

					for (int j = 0; j < numbers[i].length(); j++) {
						if (numbers[i].charAt(j) != '0'
								&& numbers[i].charAt(j) != '1') {
							return false;
						}
					}
				} else {
					return false;
				}
			}
		} else {
			return false;
		}

		return true;
	}
	
	public static void main (String[] args){
		Individuo i1 = new Individuo();
		Individuo i2 = new Individuo();
		
		System.out.println(i1+"\n"+i2);
		
		System.out.println(new Hamming(i1.getCromossomo(), i2.getCromossomo()).getHammingDistance());
		
	}
}
